from PyQt5.QtCore import *
from examples.example_calculator.calc_conf import *
#from calc_conf import *#--
from examples.example_calculator.calc_node_base import *
#from calc_node_base import *#--
from nodeeditor.utils import dumpException

class CalcGraphicsNode(QDMGraphicsNode):#节点外壳
    def initSizes(self):
        super().initSizes()
        self.width = 160
        self.height = 160
        self.edge_roundness = 6
        self.edge_padding = 0
        self.title_horizontal_padding = 8
        self.title_vertical_padding = 10

    def initAssets(self):
        super().initAssets()
        self.icons = QImage("icons/status_icons.png")

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        super().paint(painter, QStyleOptionGraphicsItem, widget)

        offset = 24.0
        if self.node.isDirty(): offset = 0.0
        if self.node.isInvalid(): offset = 48.0

        painter.drawImage(
            QRectF(-10, -10, 24.0, 24.0),
            self.icons,
            QRectF(offset, 0, 24.0, 24.0)
        )

class CalcDemoContent(QDMNodeContentWidget):#节点内容
    def initUI(self):
        self.cbx = QCheckBox("0000",self)
        self.cbx.setObjectName(self.node.content_label_objname)
    
    def serialize(self):
        res = super().serialize()
        res['value'] = self.cbx.isChecked()
        return res

    def deserialize(self, data, hashmap={}):
        res = super().deserialize(data, hashmap)
        try:
            value = data['value']
            if(value):
                self.cbx.setCheckState(Qt.Checked)
            else:
                self.cbx.setCheckState(Qt.Unchecked)
            return True & res
        except Exception as e:
            dumpException(e)
        return res

@register_node(OP_NODE_Demo)
class CalcNode_Demo(CalcNode):#节点抽象
    icon = "icons/demo.png"
    op_code = OP_NODE_Demo
    op_title = "测试Demo标题"
    content_label_objname = "calc_node_bg"
    GraphicsNode_class = CalcGraphicsNode

    def __init__(self, scene, inputs=[1,1,1], outputs=[1]):
        super().__init__(scene, inputs, outputs)
    
    def initInnerClasses(self):
        self.content = CalcDemoContent(self)
        self.grNode = CalcGraphicsNode(self)
    
    def evalImplementation(self):
        i1 = self.getInput(0)
        i2 = self.getInput(1)
        i3 = self.getInput(2)

        if i1 is None or i2 is None or i3 is None:
            self.markInvalid()
            self.markDescendantsDirty()
            self.grNode.setToolTip("Connect all inputs")
            return None

        else:
            val = i1.eval() + i2.eval() + i3.eval()
            self.value = "123hhh"
            self.markDirty(False)
            self.markInvalid(False)
            self.grNode.setToolTip("")

            self.markDescendantsDirty()
            self.evalChildren()

            return val